package com.lz.football.module.club
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ClubConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.ClubEvent;
	import com.liangzhong.events.MainEvent;
	import com.liangzhong.events.PlayerCenterEvent;
	import com.liangzhong.message.http.JsClient;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.buffer.vo.BufferVo;
	import com.liangzhong.model.club.ClothAttributeAdd;
	import com.liangzhong.model.club.vo.ClubInformation;
	import com.liangzhong.model.club.vo.TrophyVo;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.playerCenter.vo.PlayerVo;
	import com.liangzhong.model.type.GoodsType;
	import com.liangzhong.model.type.OtherGoodType;
	import com.lz.football.module.club.item.BaseItem;
	import com.lz.football.module.explore.item.PlayerBaseItem;
	import com.lz.football.module.playerCenter.ListItem;
	
	import common.BaseView.BaseView;
	import common.BaseView.DisplayMouseEventManager;
	import common.base.GoodsUsePanel;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	
	/**
	 * 俱乐部  
	 * @author Administrator
	 * 
	 */	
	public class ClubView extends BaseView implements IClubView
	{
		/**
		 *俱乐部名称  
		 */
		private var clubName:TextField;
		/**
		 *等级 
		 */
		private var Lv:TextField;
		/**
		 *国家  
		 */
		private var country:TextField;
		/**
		 *联盟 
		 */
		private var alliance:TextField;
		/**
		 *球衣属性加成 
		 */
		private var attribute:TextField;
		/**
		 *欧元 
		 */
		private var coin:TextField;
		/**
		 点券 
		 */
		private var cashpoint:TextField;
		/**
		 * 声望 
		 */
		private var fame:TextField;
		/**
		 *综合实力 
		 */
		private var strength:TextField;
		/**
		 * 奖杯容器 
		 */
		private var trophyContain:MovieClip;
		/**
		 *俱乐部信息 
		 */
		private var _clubInfo:ClubInformation;
		/**
		 *球衣 
		 */
		private var _clothContain:MovieClip;
		
		//		/**
		//		 *队徽 
		//		 */
		//		private var _logo:LogoItem;
		//		/**
		//		 *球衣 
		//		 */
		//		private var _shirt:BaseItem;
		/**
		 * 
		 */
		private var _logoView:ClubLogoView;
		/**
		 * 球衣界面
		 */
		private var _shirtView:ClubShirtView;
		//		/**
		//		 *欧元 
		//		 */
		//		private var coinIcon:BaseItem;
		//		/**
		//		 *声望 
		//		 */
		//		private var fameIcon:BaseItem;
		//		/**
		//		 *点券 
		//		 */
		//		private var cashpointIcon:BaseItem;
		
		
		/**
		 *队徽 
		 */
		private var icon:MovieClip;
		/**
		 *球员窗口 
		 */
		private var playerContain:MovieClip;
		/**
		 * 球衣按钮 
		 */
		private var clothBtn:SimpleButton;
		/**
		 *球员中心按钮 
		 */
		private var playerCenterBtn:SimpleButton;
		/**
		 *充值按钮 
		 */
		private var topupBtn:SimpleButton;
		/**
		 *队徽 
		 */
		private var logoBtn:SimpleButton;
		
		
		/**
		 *士气 
		 */
		private var _morale:TextField;
		
		/**
		 *增加士气按钮 
		 */
		private var _addBtn:SimpleButton;
		
		/**
		 * 
		 */
		private var _moraleBar:MovieClip;
		private var _moraleShape:Shape;
		
		/**
		 *声望图标  
		 */
		private var fameMc:MovieClip;
		/**
		 *国家图标  
		 */
		private var countryIcon:MovieClip;
		/**
		 *国家挑战按钮 
		 */
		private var changeNationBtn:SimpleButton;
		/**
		 *更新国家界面 
		 */
		private var _changeNationView:ChangeNationView;
		
		public function ClubView()
		{
			super();
		}
		
		override public function initUI(view:Object=null):void
		{
			//			_windowBg = Reflection.createInstance("com.lz.modul.club.Bg");
			//			this.addChild(_windowBg);
			symbolSkin = Reflection.createInstance("com.lz.modul.club.clubMainView");
			this.addChild(symbolSkin);
			_windowBg = symbolSkin.clubBg;
			countryIcon = symbolSkin.countryIcon;
			changeNationBtn = symbolSkin.changeNationBtn;
			//			_windowBg = symbolSkin;
			clubName = symbolSkin.clubName;
			Lv = symbolSkin.Lv;
			country = symbolSkin.country;
			alliance = symbolSkin.alliance;
			coin = symbolSkin.coin;
			cashpoint = symbolSkin.cashpoint
			fame = symbolSkin.fame;
			strength = symbolSkin.strength;
			trophyContain = symbolSkin.trophyContain;
			attribute = symbolSkin.attribute;
			playerContain = symbolSkin.playerContain;
			icon = symbolSkin.icon;
			_clothContain = symbolSkin.clothContain;
			clothBtn = symbolSkin.clothBtn;
			playerCenterBtn = symbolSkin.playerCenterBtn;
			topupBtn = symbolSkin.topupBtn;
			logoBtn = symbolSkin.logoBtn;
			fameMc = symbolSkin.fameMc
			
			_morale = symbolSkin._morale;
			_addBtn = symbolSkin._addBtn;
			_moraleBar = symbolSkin._moraleBar;
			
			//			_logo = new LogoItem();
			//			_logo.initUI(symbolSkin.logo);
			//			_logo.callBack = openLogo;
			//			_logo.tips = "点击打开队徽界面";
			
			//			
			//			coinIcon = new BaseItem();
			//			coinIcon.initUI(symbolSkin.coinIcon);
			//			coinIcon.tips = "欧元";
			//			
			//			fameIcon = new BaseItem();
			//			fameIcon.initUI(symbolSkin.fameIcon);
			//			fameIcon.tips = "声望";
			//			
			//			cashpointIcon = new BaseItem();
			//			cashpointIcon.initUI(symbolSkin.cashpointIcon);
			//			cashpointIcon.tips = "点券";
			
			
			attribute.htmlText = "";
			isDrag = true;
			isCloseBtn = true;
			moveClose(symbolSkin.width - 30,30);
			_clubInfo = AppData.getInstance().clubDatamanager.clubInfo;
			super.initUI();
			initData();
			setShirt();
			//			settrophy();
			//			addEvtListener();
			//			getChangeLogo({status:true});
			setPlayer();
			seticon();
			addmorale(_clubInfo.morale,null);
		}
		/**
		 * 
		 * 
		 */
		private function initData():void
		{
			
			if(_clubInfo == null)return;
			clubName.text = _clubInfo.uname;
			country.text = _clubInfo.countryToStr + "";
			countryIcon.gotoAndStop(_clubInfo.country);
			Lv.text = _clubInfo.level + "";
			alliance.htmlText = "<font color = '#FFFFFF'> <a href='event:测试'>"+(_clubInfo.unionid <=0? "未开放" : "未开放") +"</a></font>";
			
			alliance.addEventListener(TextEvent.LINK, linkHandler);
			
			coin.text = _clubInfo.coin + "";
			cashpoint.text = _clubInfo.cashpoint + "";
			fame.text = _clubInfo.fame + "";
			
			strength.text = _clubInfo.ability + "";
			//			NumberUtils.numberToContain(coin,_clubInfo.coin,2);
			//			NumberUtils.numberToContain(cashpoint,_clubInfo.cashpoint,1.5);
			//			NumberUtils.numberToContain(fame,_clubInfo.fame,1.5);
			//			NumberUtils.numberToContain(strength,_clubInfo.ability,5);
			var filter:GlowFilter = new GlowFilter(0x000000);
			filter.blurX = 4;
			filter.blurY = 4;
			filter.strength = 3;
			filter.quality = BitmapFilterQuality.HIGH;
			//			filter.knockout = true;
			//			strength.filters = [filter];
			//			coin.filters = [filter];
			//			cashpoint.filters = [filter];
			//			fame.filters = [filter];
			country.width = country.textWidth + 3;
			DisplayMouseEventManager.instance.addTipsDisplay(countryIcon,ClubConst.nationDes(_clubInfo.country));
		}
		
		/**
		 *增加士气  
		 * 
		 */
		public function addmorale($value:int,$obj:Object):void
		{
			if(_moraleShape == null)
			{
				_moraleShape = new Shape();
				_moraleShape.graphics.clear();
				_moraleShape.graphics.beginFill(0xFFFFFF);
				_moraleShape.graphics.drawRect(0,0,_moraleBar.width,_moraleBar.height);
				_moraleShape.graphics.endFill();
				_moraleBar.addChild(_moraleShape);
				_moraleBar.mask = _moraleShape;
			}
			_moraleShape.width = $value > 10? 10: $value / 10 * _moraleBar.width;
			_morale.text = $value.toString();
			if($obj != null && GoodsUsePanel.inistance.parent)
			{
				GoodsUsePanel.inistance.goodsList = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(GoodsType.OTHER,OtherGoodType.MORALE);
				GoodsUsePanel.inistance.callBack = usemoraleGoods;
				GoodsUsePanel.inistance.showBtn(2);
			}
			
		}
		
		protected function linkHandler(event:TextEvent):void
		{
			//			trace(event.text);
			Alert.showErrorTip("暂未开放，敬请期待");
		}
		/**
		 *设置球衣数据 
		 * 
		 */
		private function setShirt():void
		{
			//			_shirt = new BaseItem();
			//			_shirt.initUI(symbolSkin.poloshirt);
			//			_shirt.callBack = openShirt;
			//			_shirt.tips = "点击打开球衣界面";
			//			getChangeShirt({status:true});
		}
		private function seticon():void
		{
			DisplayObjectUtil.removeAllChildren(icon);
			var iconMc:MovieClip = Reflection.createInstance(_clubInfo.logoUrl);
			iconMc.scaleX = 0.45;
			iconMc.scaleY = 0.45;
			iconMc.x = (-iconMc.width) >> 1;
			iconMc.y = (-iconMc.height)>> 1;
			icon.addChild(iconMc);
			var goodsVo:GoodsVo = AppData.getInstance().goodsManager.getTrikotById(_clubInfo.homekot);
			if(goodsVo == null)return;
			DisplayObjectUtil.removeAllChildren(_clothContain);
			//			_shirt.setImageSorce(goodsVo.bitmapData);
			var bit:Bitmap = new Bitmap(goodsVo.bitmapData);
			_clothContain.addChild(bit);
			var buff:BufferVo = AppData.getInstance().bufferManager.getBufferById(goodsVo.buffid);
			attribute.htmlText = ClothAttributeAdd.attributeToStr(buff);
		}
		/**
		 *设置当家球星 
		 * 
		 */
		private function setPlayer():void
		{
			var arr:Array = AppData.getInstance().playerCenterDataManager.playerList;
//			arr.sortOn("ability",Array.DESCENDING);
			arr.sort(onsort);
			function onsort(a:PlayerVo,b:PlayerVo):Number
			{
				if(a.ability < b.ability)return 1;
				if(a.ability > b.ability)return -1;
				return 0;
			}
			var item:ListItem;
			var vo:PlayerVo;
			for(var i:int = 0;i < 3;i++)
			{
				vo = arr[i];
				item = new ListItem();
				item.initUI();
				item.removeBar();
				item.data = arr[i];
				item.isShowTips = true;
				item.y = /*playerContain.numChildren **/i * (item.height + 2) 
				//				item.x = 375;
				symbolSkin["plyerName_" + i].text = "" + vo.starVo.pname;
				symbolSkin["place_" + i].text = "" + vo.place_String;
				symbolSkin["ability_" + i].text = "" + vo.ability;
				playerContain.addChild(item);
			}
		}
		
		
		/**
		 *设置奖杯 
		 * 
		 */
		private function settrophy():void
		{
			var arr:Array = AppData.getInstance().clubDatamanager.trophyData;
			var item:BaseItem;
			var len:int = arr.length;
			var trophyVo:TrophyVo;
			for(var i:int = 0;i < len;i++)
			{
				trophyVo = arr[i];
				item = new BaseItem();
				item.initUI("com.lz.commonUI.ItemFrame");
				item.setImageSorce(trophyVo.url);
				item.tips = trophyVo.des;
				//				item.x = (trophyContain.numChildren %6)* 58;
				//				item.y = int(trophyContain.numChildren /6 ) *58;
				//				trophyContain.addChild(item);
			}
		}
		override public function addEvtListener():void
		{
			super.addEvtListener();
			clothBtn.addEventListener(MouseEvent.CLICK,onOpenclothHander);
			playerCenterBtn.addEventListener(MouseEvent.CLICK,onOpenPlayerCenterHandler);
			topupBtn.addEventListener(MouseEvent.CLICK,onTopupHandler);
			logoBtn.addEventListener(MouseEvent.CLICK,onOpenLogoHandler);
			_addBtn.addEventListener(MouseEvent.CLICK,onAddBtnHandler);
			DisplayMouseEventManager.instance.addTipsDisplay(fameMc,"打联赛可以获得声望！");
			changeNationBtn.addEventListener(MouseEvent.CLICK,onChangeNationHanlder);
			
			if(_clubInfo.countrymodify == 0)
			{
				DisplayMouseEventManager.instance.addTipsDisplay(changeNationBtn,"第一次可免费变更");
			}else
			{
				DisplayMouseEventManager.instance.addTipsDisplay(changeNationBtn,"花费688点券可进行一次国家变更");
			}
		}
		
		/**
		 *更新国家 
		 * @param event
		 * 
		 */
		protected function onChangeNationHanlder(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(_clubInfo.level <10)
			{
				Alert.showErrorTip("俱乐部10级才可以更换国家！");
				return;
			}
			if(_changeNationView == null)
			{
				_changeNationView = new ChangeNationView();
				_changeNationView.initUI();
			}
			GlobalDef.GameWindowContainer.addChild(_changeNationView);
		}
		/**
		 * 增加士气 
		 * @param event
		 * 
		 */
		private function onAddBtnHandler(event:MouseEvent):void
		{
			if(_clubInfo.morale >= 10)
			{
				Alert.showErrorTip("士气已满，不需要增加士气");
				return;
			}
			GoodsUsePanel.inistance.initPlace();
			GoodsUsePanel.inistance.setShowGoodsType(GoodsType.OTHER,OtherGoodType.MORALE);
			GoodsUsePanel.inistance.callBack = usemoraleGoods;
			GoodsUsePanel.inistance.showBtn(2);
		}
		/**
		 * 使用士气 物品 
		 * @param $goodsVo
		 * 
		 */
		private function usemoraleGoods($goodsVo:GoodsVo):void
		{
			if($goodsVo == null)
			{
				Alert.show("确定花费100点券来增加士气么？","",Alert.NO,GlobalDef.AlertContainer,useItem)
				function useItem($type:int):void
				{
					if($type == Alert.YES)
						AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.ADD_MORALE,{cellId:0}));
				}
				return;
			}
			var cellId:int;
			cellId = $goodsVo.pid;
			AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.ADD_MORALE,{cellId:cellId}));
		}
		
		protected function onOpenLogoHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(_logoView == null)
			{
				_logoView = new ClubLogoView();
				_logoView.initUI();
			}
			GlobalDef.GameWindowContainer.addChild(_logoView);
		}
		
		protected function onTopupHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
//			Alert.showErrorTip("功能尚未开放！");
			JsClient.recharge();
		}
		
		/**
		 *打开球员中心界面 
		 * @param event
		 * 
		 */
		protected function onOpenPlayerCenterHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			AppEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.OPEN_PLAYERCENTER));
		}
		
		/**
		 *打开球衣界面 
		 * @param event
		 * 
		 */
		protected function onOpenclothHander(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(_shirtView == null)
			{
				_shirtView = new ClubShirtView()
				_shirtView.initUI();
			}
			
			GlobalDef.GameWindowContainer.addChild(_shirtView);
		}		
		
		
		override public function removeEvtListener():void
		{
			super.removeEvtListener()
			clothBtn.removeEventListener(MouseEvent.CLICK,onOpenclothHander);
			playerCenterBtn.removeEventListener(MouseEvent.CLICK,onOpenPlayerCenterHandler);
			topupBtn.removeEventListener(MouseEvent.CLICK,onTopupHandler);
			logoBtn.removeEventListener(MouseEvent.CLICK,onOpenLogoHandler);
			alliance.removeEventListener(TextEvent.LINK, linkHandler);
			DisplayMouseEventManager.instance.removeTipsDisplay(countryIcon);
			_addBtn.removeEventListener(MouseEvent.CLICK,onAddBtnHandler);
			DisplayMouseEventManager.instance.removeTipsDisplay(fameMc);
			changeNationBtn.removeEventListener(MouseEvent.CLICK,onChangeNationHanlder);
			DisplayMouseEventManager.instance.removeTipsDisplay(changeNationBtn);
		}
		
		override 	public function destroyView():void
		{
			removeEvtListener();
			//			DisplayObjectUtil.removeAllChildren(coin);
			//			DisplayObjectUtil.removeAllChildren(cashpoint);
			//			DisplayObjectUtil.removeAllChildren(fame);
			//			DisplayObjectUtil.removeAllChildren(strength);
			//			_logo.destroyView();
			//			_logo = null;
			//			_shirt.destroyView();
			//			_shirt = null;
			//			coinIcon.destroyView();
			//			coinIcon = null;
			//			fameIcon.destroyView();
			//			fameIcon = null;
			//			cashpointIcon.destroyView();
			//			cashpointIcon = null;
			var item:ListItem;
			while(playerContain.numChildren)
			{
				item = playerContain.removeChildAt(0) as ListItem;
				item.destroyView();
				item = null;
			}
			
			DisplayObjectUtil.removeAllChildren(icon);
			DisplayObjectUtil.removeAllChildren(_clothContain);
			if(_logoView != null)
			{
				_logoView.destroyView();
			}
			if(_shirtView != null)
			{
				_shirtView.destroyView();
			}
			if(_changeNationView != null)
			{
				_changeNationView.destroyView();
					
			}
			_changeNationView = null;
			if(GoodsUsePanel.inistance.parent)
				GoodsUsePanel.inistance.parent.removeChild(GoodsUsePanel.inistance);
			super.destroyView();
			symbolSkin = null;
		}
		
		override public function showType($obj:Object):void
		{
		}
		override public function onCloseHandler(event:MouseEvent):void
		{
			AppEventDispatcher.getInstance().dispatchEvent(new ClubEvent(ClubEvent.REMOVE_MODE));
		}
		/**
		 *返回修改队徽 
		 * 
		 */
		public function getChangeLogo($obj:Object):void
		{
			if(_logoView != null)_logoView.getChangeLogo();
			if(!$obj.status)return;
			//			_logo.setImageSorce(_clubInfo.logoUrl);
			seticon();
		}
		
		/**
		 *返回修改球衣 
		 * 
		 */
		public function getChangeShirt($obj:Object):void
		{
			if(_shirtView != null)_shirtView.getChangeShirt();
			if(!$obj.status)return;
			
			var goodsVo:GoodsVo = AppData.getInstance().goodsManager.getTrikotById(_clubInfo.homekot);
			if(goodsVo == null)return;
			//			_shirt.setImageSorce(goodsVo.bitmapData);
			var buff:BufferVo = AppData.getInstance().bufferManager.getBufferById(goodsVo.buffid);
			attribute.htmlText = ClothAttributeAdd.attributeToStr(buff);
			DisplayObjectUtil.removeAllChildren(_clothContain);
			var bit:Bitmap = new Bitmap(goodsVo.bitmapData);
			_clothContain.addChild(bit);
		}
		/**
		 *获得一件球衣 
		 * @param $obj
		 * 
		 */
		public function getOntShirt($obj:Object):void
		{
			if(_shirtView != null)
			{
				_shirtView.getOneShirt($obj);
			}
		}
		/**
		 * 经济值改变
		 * @param $obj
		 * 
		 */
		public function economyvaluechange($obj:Object):void
		{
			coin.text = _clubInfo.coin + "";
			cashpoint.text = _clubInfo.cashpoint + "";
			fame.text = _clubInfo.fame + "";
			
			strength.text = _clubInfo.ability + "";
		}
		/**
		 *增加士气  
		 * @param $obj
		 * 
		 */
		public function addMornal($obj:Object):void
		{
			addmorale(_clubInfo.morale,$obj);
		}
		/**
		 *返回更换国家 
		 * @param $obj
		 * 
		 */
		public function getChangeNation($obj:Object):void
		{
			country.text = _clubInfo.countryToStr + "";
			countryIcon.gotoAndStop(_clubInfo.country);
			
			if(_clubInfo.countrymodify == 0)
			{
				DisplayMouseEventManager.instance.addTipsDisplay(changeNationBtn,"第一次可免费变更");
			}else
			{
				DisplayMouseEventManager.instance.addTipsDisplay(changeNationBtn,"花费688点券可进行一次国家变更");
			}
			if(_changeNationView != null)
			{
				
			}
		}
	}
}